using System;

namespace RealtimeCollabGame.Domain.ValueObjects
{
    public record Position(int X, int Y)
    {
        public static Position Origin => new(0, 0);
        
        public static Position Create(int x, int y)
        {
            if (x < 0) throw new ArgumentException("X坐标不能为负数", nameof(x));
            if (y < 0) throw new ArgumentException("Y坐标不能为负数", nameof(y));
            
            return new Position(x, y);
        }
        
        public double DistanceTo(Position other)
        {
            return Math.Sqrt(Math.Pow(X - other.X, 2) + Math.Pow(Y - other.Y, 2));
        }
        
        public bool IsWithinBounds(int maxWidth, int maxHeight)
        {
            return X >= 0 && X < maxWidth && Y >= 0 && Y < maxHeight;
        }
        
        public Position Add(int deltaX, int deltaY)
        {
            return new Position(X + deltaX, Y + deltaY);
        }
        
        public override string ToString()
        {
            return $"({X}, {Y})";
        }
    }
}